﻿using System;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;
namespace CommunicationProtocol.dto.command.client
{
    public class OtherPlayerPlantingCommand:Command
    {
        public short commandObjectId { get; }
        public int xPixel { get; }
        public int yPixel { get; }
        /**
         * 是谁正在操作这个对象
         */
        public string operatorId { get; }

        /*
         * 1 代表创建这个对象
         * 2 移动这个对象
         * 3 移除这个对象
         */
        public byte status { get; }

        public OtherPlayerPlantingCommand(short commandObjectId, int xPixel, int yPixel) : base(6)
        {
            this.commandObjectId = commandObjectId;
            this.xPixel = xPixel;
            this.yPixel = yPixel;
            status = 2;
        }

        public OtherPlayerPlantingCommand(short objectId, byte status) : base(6)
        {
            this.commandObjectId = objectId;
            this.status = status;
        }

        public OtherPlayerPlantingCommand(byte[] remainingBytes) : base(6)
        {
            commandObjectId = BitConverter.ToInt16(remainingBytes, 0);
            status = remainingBytes[2];
            xPixel = BitConverter.ToInt32(remainingBytes, 3);
            yPixel = BitConverter.ToInt32(remainingBytes, 7);
            if(remainingBytes.Length > 11){ 
                this.operatorId = Encoding.UTF8.GetString(remainingBytes,11,remainingBytes.Length-11);
            }
        }

        public OtherPlayerPlantingCommand(byte[] remainingBytes, string userId) : this(remainingBytes)
        {
            this.operatorId = userId;
        }

        public override byte[] GetBytes()
        {
            System.Collections.Generic.IEnumerable<byte> enumerable = new[] { functionCode }
                            .Concat(BitConverter.GetBytes(commandObjectId))
                            .Concat(new byte[]{ status})
                            .Concat(BitConverter.GetBytes(xPixel))
                            .Concat(BitConverter.GetBytes(yPixel));

            if (!(operatorId is null) ){
                enumerable = enumerable
                    .Concat(Encoding.UTF8.GetBytes(operatorId));
            }

            return enumerable.ToArray();
        }

        
    }
}